package com.king.wms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.king.wms.mapper.OperatorMapper;
import com.king.wms.pojo.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.king.wms.pojo.Loc;
import com.king.wms.pojo.Sku;
import com.king.wms.service.LocService;
import com.king.wms.service.SkuService;

@Controller
@RequestMapping(value = "/loc")
public class LocController {

	@Autowired
	private LocService locService;
	
	@Autowired
	private SkuService skuService;

	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Loc> list_loc = locService.findAll();
		
		List<Sku> list_sku = skuService.findAll();
		
		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_sku", list_sku);
		
		request.getRequestDispatcher("/jsp/loc/loc.jsp").forward(request,
				response);

	}
	
	@RequestMapping(value = "/findSkuByloc_id/{loc_id}")
	public void findSkuByloc_id(@PathVariable("loc_id") Integer loc_id,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		List<Sku> list_sku = skuService.findSkuByloc_id(loc_id);
		System.out.println("========================="+list_sku);
		
		request.setAttribute("loc_id", loc_id);
		request.setAttribute("list_sku", list_sku);
		request.getRequestDispatcher("/jsp/loc/locSku.jsp").forward(request,
				response);
		
	}

	@RequestMapping(value = "/delete/{loc_id}")
	public String delete(@PathVariable("loc_id") Integer loc_id) {
		locService.delete(loc_id);
		return "redirect:/loc/findAll";
	}

	@RequestMapping(value = "/insert")
	public String insert(Loc loc) {
		
		Integer stackremianed =  loc.getStacklimit() - loc.getStackuesd();
		loc.setStackremianed(stackremianed);
		
		locService.insert(loc);
		return "redirect:/loc/findAll";
	}

	@RequestMapping("/toUpdateLocPage/{loc_id}")
	public void toUpdateLocPage(@PathVariable("loc_id") Integer loc_id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		Loc loc = locService.findLocByLoc(loc_id);
		request.setAttribute("loc", loc);
		request.getRequestDispatcher("/jsp/loc/updateLoc.jsp").forward(request,
				response);

	}

	@RequestMapping(value = "/update")
	public String update(Loc loc,
			HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
		
		String message = locService.update(loc);
		
		if("success".equals(message)){
			return "redirect:/loc/findAll";
		}else
		{
			request.setAttribute("message", message);
			
			return "error";
		}
	}

@Autowired
private OperatorMapper operatorMapper;

	@RequestMapping(value = "/findAll_ope")
	public void findAll_ope(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		List<Operator> list_ope = operatorMapper.findAll_ope();

		request.setAttribute("list_ope", list_ope);

		request.getRequestDispatcher("/jsp/loc/ope.jsp").forward(request,
				response);

	}

	@RequestMapping(value = "/insert_ope")
	public String insert_ope(Operator loc) {

		operatorMapper.insert(loc);
		return "redirect:/loc/findAll_ope";
	}

	@RequestMapping(value = "/update_ope")
	public String update_ope(Operator loc,
						 HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		operatorMapper.updateByPrimaryKeySelective(loc);
		return "redirect:/loc/findAll_ope";
	}

	@RequestMapping(value = "/delete_ope/{id}")
	public String delete_ope(@PathVariable("id") Integer id) {
		operatorMapper.deleteByPrimaryKey(id);
		return "redirect:/loc/findAll_ope";
	}

	@RequestMapping("/toUpdateLocPage_ope/{id}")
	public void toUpdateLocPage_ope(@PathVariable("id") Integer id,
								HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		Operator loc = operatorMapper.selectByPrimaryKey(id);
		request.setAttribute("loc", loc);
		request.getRequestDispatcher("/jsp/loc/updateLoc_ope.jsp").forward(request,
				response);

	}


	@RequestMapping(value = "/qx")
	public void qx(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		List<Operator> list_ope = operatorMapper.findAll_qx();

		request.setAttribute("list_ope", list_ope);

		request.getRequestDispatcher("/jsp/loc/qx.jsp").forward(request,
				response);

	}

	@RequestMapping("/toUpdateLocPage_qx/{id}")
	public void toUpdateLocPage_qx(@PathVariable("id") Integer id,
									HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		Operator loc = operatorMapper.selectByPrimaryKey(id);
		request.setAttribute("loc", loc);
		request.getRequestDispatcher("/jsp/loc/updateLoc_qx.jsp").forward(request,
				response);

	}

	@RequestMapping(value = "/update_qx")
	public String update_qx(Operator loc,
							 HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		operatorMapper.updateByPrimaryKeySelective(loc);
		return "redirect:/loc/qx";
	}
}
